package com.ssy.lingxi.after.sale.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.after.sale.constant.Constants;
import com.ssy.lingxi.after.sale.entity.*;
import com.ssy.lingxi.after.sale.model.constant.AfterSaleTypeEnum;
import com.ssy.lingxi.after.sale.model.constant.ReturnGoodsInnerStatusEnum;
import com.ssy.lingxi.after.sale.model.constant.ReturnGoodsOuterStatusEnum;
import com.ssy.lingxi.after.sale.repository.InnerWorkflowRecordRepository;
import com.ssy.lingxi.after.sale.repository.OuterWorkflowRecordRepository;
import com.ssy.lingxi.after.sale.repository.ReturnGoodsDetailRepository;
import com.ssy.lingxi.after.sale.service.IFeignService;
import com.ssy.lingxi.after.sale.service.IProcessFeignService;
import com.ssy.lingxi.common.constant.member.MemberRoleEnum;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.message.api.constant.MessageTemplateCode;
import com.ssy.lingxi.message.api.vo.request.SystemMessageRequest;
import com.ssy.lingxi.order.api.model.vo.request.OrderAfterSaleVO;
import com.ssy.lingxi.order.api.model.vo.request.OrderProductAfterSaleVO;
import com.ssy.lingxi.report.api.enums.AfterSaleReturnApplyOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.AfterSaleReturnHandleOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.AfterSaleReturnPfHandleOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import com.ssy.lingxi.workflow.api.model.contant.ProcessEnum;
import com.ssy.lingxi.workflow.api.model.vo.request.TaskExecuteVO;
import com.ssy.lingxi.workflow.api.model.vo.response.SimpleTaskCompleteVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  平台后台-系统管理-操作日志
 * @author ds
 * @version 2.0.0
 * @date 2021/06/17
 */
@Slf4j
@Service
public class BaseReturnGoodsServiceImpl {
    @Resource
    private OuterWorkflowRecordRepository outerWorkflowRecordRepository;
    @Resource
    private InnerWorkflowRecordRepository innerWorkflowRecordRepository;
    @Resource
    private ReturnGoodsDetailRepository returnGoodsDetailRepository;
    @Resource
    private IFeignService feignService;
    @Resource
    private IProcessFeignService processFeignService;

    private List<OrderAfterSaleVO> convertOrderProductMapToList(Map<Long, List<OrderProductAfterSaleVO>> orderProductMap){
        // 更新售后商品数量
        List<OrderAfterSaleVO> afterSales=new ArrayList<>();
        orderProductMap.forEach((k,v)->{
            OrderAfterSaleVO orderVO=new OrderAfterSaleVO();
            orderVO.setOrderId(k);
            orderVO.setProducts(v);
            afterSales.add(orderVO);
        });
        return afterSales;
    }

    /**
     * 构建订单更新VO
     * @param returnId 退货单id
     * @return 构建订单更新vo
     */
    protected List<OrderAfterSaleVO> buildUpdateOrderParamBySave(Long returnId){
        List<ReturnGoodsDetailDO> goodsDetailDOS = returnGoodsDetailRepository.findByReturnId(returnId);
        Map<Long, List<OrderProductAfterSaleVO>> orderProductMap=new HashMap<>();
        goodsDetailDOS.forEach(r ->{
            List<OrderProductAfterSaleVO> orderProductList = orderProductMap.get(r.getOrderId());
            double returnCount =  -r.getReturnCount();
            double amount = -r.getRefundList().stream().mapToDouble(ReturnGoodsRefundDetailDO::getRefundAmount).sum();
            if(CollectionUtil.isEmpty(orderProductList)){
                orderProductList=new ArrayList<>();
                OrderProductAfterSaleVO vo=new OrderProductAfterSaleVO();
                vo.setReturnCount(BigDecimal.valueOf(returnCount));
                vo.setReturnAmount(BigDecimal.valueOf(amount));
                vo.setProductId(r.getOrderRecordId());
                orderProductList.add(vo);
                orderProductMap.put(r.getOrderId(),orderProductList);
            }else{
                OrderProductAfterSaleVO vo=new OrderProductAfterSaleVO();
                vo.setReturnCount(BigDecimal.valueOf(returnCount));
                vo.setReturnAmount(BigDecimal.valueOf(amount));
                vo.setProductId(r.getOrderRecordId());
                orderProductList.add(vo);
            }
        });
        return convertOrderProductMapToList(orderProductMap);
    }

    /**
     * 新增外部工作流记录
     * @author 伍将
     * @date 2020/9/11
     * @param user :
     * @param repairId :
     * @param statusCode :
     * @param operate :
     * @param opinion :
     **/
    protected void addOuterWorkflowRecord(UserLoginCacheDTO user, Long repairId, Integer statusCode, String operate, String opinion) {
        OuterWorkflowRecordDO recordDO = new OuterWorkflowRecordDO();
        recordDO.setAfterSaleId(repairId);
        recordDO.setAfterSaleType(AfterSaleTypeEnum.RETURN.getCode());
        recordDO.setUserId(user.getUserId());
        recordDO.setMemberId(user.getMemberId());
        recordDO.setRoleId(user.getMemberRoleId());
        recordDO.setRoleName(user.getMemberRoleName());
        recordDO.setStatus(ReturnGoodsOuterStatusEnum.getMessage(statusCode));
        recordDO.setStatusCode(statusCode);
        recordDO.setOperate(operate);
        recordDO.setOperateTime(System.currentTimeMillis());
        recordDO.setOpinion(opinion);
        outerWorkflowRecordRepository.saveAndFlush(recordDO);
    }


    protected void addInnerWorkflowRecord(UserLoginCacheDTO user, Long repairId, Integer statusCode, String statusName, String operate, String opinion) {
        InnerWorkflowRecordDO recordDO = new InnerWorkflowRecordDO();
        recordDO.setAfterSaleId(repairId);
        recordDO.setAfterSaleType(AfterSaleTypeEnum.RETURN.getCode());
        recordDO.setUserId(user.getUserId());
        recordDO.setMemberId(user.getMemberId());
        recordDO.setRoleId(user.getMemberRoleId());
        recordDO.setOperator(user.getUserName());
        recordDO.setDepartment(user.getUserOrgName());
        recordDO.setJobTitle(user.getUserJobTitle());
        recordDO.setStatus(ReturnGoodsInnerStatusEnum.getMessage(statusCode));
        recordDO.setStatusCode(statusCode);
        recordDO.setOperate(operate);
        recordDO.setOperateTime(System.currentTimeMillis());
        recordDO.setOpinion(opinion);
        innerWorkflowRecordRepository.saveAndFlush(recordDO);

    }

    private void sendSystemMessage(Long memberId,Long roleId,String messageNotice,String applyNo,String applyAbstract){
        SystemMessageRequest request = new SystemMessageRequest();
        request.setMemberId(memberId);
        request.setRoleId(roleId);
        request.setMessageNotice(messageNotice);
        List<String> params = new ArrayList<>();
        params.add(applyNo);
        params.add(applyAbstract);
        request.setParams(params);
        feignService.sendSystemMessage(request);
    }

    /**
     * 公共增加计数统计与操作通知
     * @param returnDO 退货单数据
     * @param status 更新状态
     */
    protected void commonSystemMessageAndIncreaseOperateNotice(ReturnGoodsDO returnDO, Integer status){
        OperateNoticeVO noticeVO;
        //待提交退货申请单
        if (status.equals(ReturnGoodsOuterStatusEnum.TO_BE_SUBMIT.getCode())) {
            //待提交退货申请单(采购商)+1  针对申请不通过
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_COMMIT.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增[待提交退货申请单]消息通知
            sendSystemMessage(returnDO.getMemberId(),returnDO.getRoleId(), MessageTemplateCode.return_goods_apply,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }
        //待平台审核退货申请单
        else if (status.equals(ReturnGoodsOuterStatusEnum.TO_BE_PLATFORM_CONFIRM.getCode())) {
            //待平台确认退货申请单(平台)+1
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(MemberRoleEnum.PLATFORM.getMemberId());
            noticeVO.setRoleId(MemberRoleEnum.PLATFORM.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.PLATFORM_AFTER_SALE.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnPfHandleOperateTypeEnum.TO_BE_CONFIRM.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增[待确认退货申请单]消息通知
            sendSystemMessage(MemberRoleEnum.PLATFORM.getMemberId(),MemberRoleEnum.PLATFORM.getRoleId(), MessageTemplateCode.return_goods_handle_platform_confirm,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }
        //待新增退货发货单
        else if (status.equals(ReturnGoodsOuterStatusEnum.TO_BE_ADD_RETURN_DELIVERY_GOODS.getCode())) {
            //待新增发货单
            //待售后退货申请(采购商)新增退货发货单+1
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_ADD_DELIVERY.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增[待新增退货发货单]消息通知
            sendSystemMessage(returnDO.getMemberId(),returnDO.getRoleId(),MessageTemplateCode.return_goods_shipment,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }else if (status.equals(ReturnGoodsOuterStatusEnum.TO_BE_RETURN_DELIVERY_GOODS.getCode())) {
            //待确认发货
            //待售后退货申请(采购商)退货发货+1
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_DELIVERY.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增[待确认售后完成]消息通知
            sendSystemMessage(returnDO.getMemberId(),returnDO.getRoleId(),MessageTemplateCode.return_goods_return_shipment,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }else if(status.equals(ReturnGoodsOuterStatusEnum.TO_BE_RETURN_RECEIVE_GOODS.getCode())){
            //待退货收货
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(returnDO.getParentMemberId());
            noticeVO.setRoleId(returnDO.getParentMemberRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_HANDLE.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnHandleOperateTypeEnum.TO_BE_RECEIVE.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增[待退货收货]消息通知
            sendSystemMessage(returnDO.getParentMemberId(),returnDO.getParentMemberRoleId(),MessageTemplateCode.return_goods_handle_return_receive,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }else if(status.equals(ReturnGoodsOuterStatusEnum.TO_BE_CONFIRM_RETURN_GOODS_RECEIPT.getCode())){
            //待确认退货回单
            // 售后退货申请(采购商)确认退货回单+1
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_CONFIRM_RECEIPT.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增[待确认退货回单]消息通知
            sendSystemMessage(returnDO.getMemberId(),returnDO.getRoleId(),MessageTemplateCode.return_goods_confirm_return,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }else if(status.equals(ReturnGoodsOuterStatusEnum.TO_BE_REFUND.getCode())){
            //待退款
            // 售后退货处理(供应商)待退款+1
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(returnDO.getParentMemberId());
            noticeVO.setRoleId(returnDO.getParentMemberRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_HANDLE.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnHandleOperateTypeEnum.TO_BE_REFUND.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增供应商待退款消息通知
            sendSystemMessage(returnDO.getParentMemberId(),returnDO.getParentMemberRoleId(),MessageTemplateCode.return_goods_handle_refund,returnDO.getApplyNo(),returnDO.getApplyAbstract());
            // 新增[平台待退款]消息通知
            sendSystemMessage(MemberRoleEnum.PLATFORM.getMemberId(),MemberRoleEnum.PLATFORM.getRoleId(),MessageTemplateCode.return_goods_handle_platform_refund,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }else if(status.equals(ReturnGoodsOuterStatusEnum.TO_BE_CONFIRM_REFUND.getCode())){
            //待确认退款
            // 售后退货处理(采购商)待确认退款+1
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_HANDLE.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_CONFIRM_REFUND.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增供应商待确认退款消息通知
            sendSystemMessage(returnDO.getMemberId(),returnDO.getRoleId(),MessageTemplateCode.return_goods_confirm_refund,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }else if(status.equals(ReturnGoodsOuterStatusEnum.TO_BE_CONFIRM_COMPLETE.getCode())){
            //待确认售后完成
            // 售后退货申请(采购商)确认售后完成+1
            noticeVO = new OperateNoticeVO();
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setIncreaseOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_CONFIRM_COMPLETE.getCode());
            noticeVO.setIncreaseCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setIncreaseTitle(returnDO.getApplyAbstract());
            noticeVO.setIncreaseDataId(returnDO.getId());
            feignService.addSysOperateNotice(noticeVO);
            // 新增[待确认售后完成]消息通知
            sendSystemMessage(returnDO.getMemberId(),returnDO.getRoleId(),MessageTemplateCode.return_goods_confirm,returnDO.getApplyNo(),returnDO.getApplyAbstract());
        }
    }

    /**
     * 公共减少计数统计
     * @param returnDO 退货单数据
     * @param oldStatus 旧状态
     */
    protected void commonReduceOperateNotice(ReturnGoodsDO returnDO,Integer oldStatus){
        log.info("commonReduceOperateNotice: oldStatus:{}",oldStatus);
        OperateNoticeVO noticeVO = new OperateNoticeVO();
        if(ReturnGoodsOuterStatusEnum.TO_BE_SUBMIT.getCode().equals(oldStatus)){
            //待提交申请单
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_COMMIT.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_CONFIRM.getCode().equals(oldStatus)){
            //待确认申请单
            noticeVO.setMemberId(returnDO.getParentMemberId());
            noticeVO.setRoleId(returnDO.getParentMemberRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_HANDLE.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnHandleOperateTypeEnum.TO_BE_VALIFY_COMMIT.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_PLATFORM_CONFIRM.getCode().equals(oldStatus)){
            //待平台确认申请单
            noticeVO.setMemberId(MemberRoleEnum.PLATFORM.getMemberId());
            noticeVO.setRoleId(MemberRoleEnum.PLATFORM.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.PLATFORM_AFTER_SALE.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnPfHandleOperateTypeEnum.TO_BE_CONFIRM.getCode());
        }else if(ReturnGoodsOuterStatusEnum.UN_ACCEPT_APPLY.getCode().equals(oldStatus)){
            //不接受申请
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_COMMIT.getCode());
        }else if(ReturnGoodsOuterStatusEnum.ACCEPT_APPLY.getCode().equals(oldStatus)){
            //接受申请
            noticeVO.setMemberId(returnDO.getParentMemberId());
            noticeVO.setRoleId(returnDO.getParentMemberRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_HANDLE.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnHandleOperateTypeEnum.TO_BE_CONFIRM.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_ADD_RETURN_DELIVERY_GOODS.getCode().equals(oldStatus)){
            //待新增退货发货单（确认申请单后）
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_ADD_DELIVERY.getCode());
        }else if(ReturnGoodsOuterStatusEnum.CONSUMER_TO_BE_ADD_LOGISTICS.getCode().equals(oldStatus)){
            //采购商待新增物流单
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_ADD_LOGISTICS.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_RETURN_DELIVERY_GOODS.getCode().equals(oldStatus)){
            //待退货发货
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_DELIVERY.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_ADD_RETURN_GOODS_STORAGE.getCode().equals(oldStatus)){
            //待新增退货入库单
            noticeVO.setMemberId(returnDO.getParentMemberId());
            noticeVO.setRoleId(returnDO.getParentMemberRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_HANDLE.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnHandleOperateTypeEnum.TO_BE_ADD_STORAGE.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_RETURN_RECEIVE_GOODS.getCode().equals(oldStatus)){
            //待退货收货
            noticeVO.setMemberId(returnDO.getParentMemberId());
            noticeVO.setRoleId(returnDO.getParentMemberRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_HANDLE.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnHandleOperateTypeEnum.TO_BE_RECEIVE.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_CONFIRM_RETURN_GOODS_RECEIPT.getCode().equals(oldStatus)){
            //待确认退货回单
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_CONFIRM_RECEIPT.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_REFUND.getCode().equals(oldStatus)){
            //待退款
            noticeVO.setMemberId(returnDO.getParentMemberId());
            noticeVO.setRoleId(returnDO.getParentMemberRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_HANDLE.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnHandleOperateTypeEnum.TO_BE_REFUND.getCode());
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_CONFIRM_REFUND.getCode().equals(oldStatus)){
            //待确认退款(退款)
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_CONFIRM_REFUND.getCode());
        }else if(ReturnGoodsOuterStatusEnum.CONFIRM_REFUND_FAIL.getCode().equals(oldStatus)){
            //确认退款未到账
        }else if(ReturnGoodsOuterStatusEnum.TO_BE_CONFIRM_COMPLETE.getCode().equals(oldStatus)){
            //待确认售后完成
            noticeVO.setMemberId(returnDO.getMemberId());
            noticeVO.setRoleId(returnDO.getRoleId());
            noticeVO.setDataSource(OperateDataSourceEnum.AFTER_SALE_RETURN_APPLY.getCode());
            noticeVO.setReduceOperateType(AfterSaleReturnApplyOperateTypeEnum.TO_BE_CONFIRM_COMPLETE.getCode());
        }else if(ReturnGoodsOuterStatusEnum.COMPLETE.getCode().equals(oldStatus)){
            //售后完成
        }else if(ReturnGoodsOuterStatusEnum.STOPPED.getCode().equals(oldStatus)){
            //中止
        }

        if(noticeVO.getMemberId()!=null) {
            noticeVO.setReduceCount(Constants.SYS_OPERATE_NOTICE_COUNT);
            noticeVO.setReduceDataId(returnDO.getId());
            log.info("commonReduceOperateNotice: oldStatus:{},noticeVO:{}",oldStatus, JSONUtil.toJsonStr(noticeVO));
            feignService.addSysOperateNotice(noticeVO);
        }
    }

    /**
     * 执行外部工作流
     * @param entity 退货单
     * @param agree 签收人
     */
    protected void completeOuterTask(ReturnGoodsDO entity, Integer agree){
        TaskExecuteVO taskStartVO=new TaskExecuteVO();
        taskStartVO.setProcessKey(ProcessEnum.getProcessKeyByCode(entity.getOuterTaskType()));
        taskStartVO.setMemberId(entity.getMemberId());
        taskStartVO.setRoleId(entity.getRoleId());
        taskStartVO.setDataId(entity.getId());
        taskStartVO.setAgree(agree);
        taskStartVO.setTaskId(entity.getOuterTaskId());
        Wrapper<SimpleTaskCompleteVO> taskResult = processFeignService.completeSimpleTask(taskStartVO);
        if (taskResult == null || taskResult.getCode() != ResponseCode.SUCCESS.getCode()
                || null == taskResult.getData()) {
            throw new BusinessException(ResponseCode.AS_WORKFLOW_ERROR);
        }
        entity.setOuterStatus(taskResult.getData().getStatus());
        String taskId=taskResult.getData().getTaskId();
        entity.setOuterTaskId(StringUtils.hasLength(taskId)?taskId:Constants.WORK_FLOW_END_TASK_ID_TAG);
        entity.setOuterTaskStep(taskResult.getData().getStep());
        entity.setUpdateTime(System.currentTimeMillis());
    }
}
